package com.sdut.material.service.impl;


import com.sdut.material.mapper.EvaluteMapper;
import com.sdut.material.pojo.Supplier;
import com.sdut.material.pojo.SupplierMaterial;
import com.sdut.material.pojo.Survey;
import com.sdut.material.service.*;

import com.sdut.material.util.JSONResult;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class EvaluateServiceImpl  implements IEvaluateService {
    @Autowired
    private EvaluteMapper evaluteMapper;
    @Autowired
    private ISupplierMaterialService supplierMaterialService;
    @Autowired
    private ISurveyService surveyService;
    @Autowired
    private ISupplierService supplierService;
    @Override
    public JSONResult selectAndEvaluate(int materialId) {
        List<SupplierMaterial> supplierMaterials = supplierMaterialService.selectByMaterialId(materialId);
        SupplierMaterial supplierMaterialResult = new SupplierMaterial();
        double maxEvaluateScore = 0;
        for (SupplierMaterial supplierMaterial : supplierMaterials) {
            double priceScore = supplierMaterial.getPriceScore();
            double creditScore = supplierMaterial.getCreditScore();
            double saleScore = supplierMaterial.getSaleScore();
            double qualityScore = supplierMaterial.getQualityScore();
            //构建模糊互补矩阵并计算ri
            double[][] doubles = new double[4][5];
            doubles[0][0] = 0.5000;
            doubles[0][1] = priceScore / (priceScore + creditScore);
            doubles[0][2] = priceScore / (priceScore + saleScore);
            doubles[0][3] = priceScore / (priceScore + qualityScore);
            doubles[1][0] = 1 - doubles[0][1];
            doubles[1][1] = 0.5000;
            doubles[1][2] = creditScore / (creditScore + saleScore);
            doubles[1][3] = creditScore / (creditScore + qualityScore);
            doubles[2][0] = 1 - doubles[0][2];
            doubles[2][1] = 1 - doubles[1][2];
            doubles[2][2] = 0.5000;
            doubles[2][3] = saleScore / (saleScore + qualityScore);
            doubles[3][0] = 1 - doubles[0][3];
            doubles[3][1] = 1 - doubles[1][3];
            doubles[3][2] = 1 - doubles[2][3];
            doubles[3][3] = 0.5000;
            for (int i = 0; i < 4; i++) {
                double sum = 0;
                for (int j = 0; j < 4; j++) {
                    sum += doubles[i][j];
                }
                doubles[i][4] = sum;
            }
            //输出互补矩阵
            System.out.println("输出互补矩阵");
            for (int i = 0; i < 4;i++) {
                for (int j = 0;j < 5;j++) {
                    System.out.println(doubles[i][j]+",");
                }
                System.out.println();
            }
            //构建rij矩阵并计算各要素相对重要度
            double[][] Rmatrix = new double[4][5];
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    Rmatrix[i][j] = (doubles[i][4] - doubles[j][4])/6+0.5000;
                }
            }
            for (int i = 0; i < 4; i++) {
                double sum = 0;
                for (int j = 0; j < 4; j++) {
                    sum += Rmatrix[i][j];
                }
                System.out.println(sum);
                Rmatrix[i][4] = 1.0/4.0-1.0/3.0+sum/6.0;
            }
            //输出rij矩阵
            System.out.println("输出rij矩阵");
            for (int i = 0; i < 4;i++) {
                for (int j = 0;j < 5;j++) {
                    System.out.println(Rmatrix[i][j]+",");
                }
                System.out.println();
            }
            //构建问卷矩阵统计每个供应商的每个因素的选择人数
            List<Survey> surveyList = surveyService.selectAllSurveys();
            double[][] surveyTotal = new double[4][5];
            for (Survey survey : surveyList) {
                int priceEvaluate = survey.getPriceEvaluate();
                if (priceEvaluate == 1) {
                    surveyTotal[0][0] += 1;
                } else if (priceEvaluate == 2) {
                    surveyTotal[0][1] += 1;
                } else if (priceEvaluate == 3) {
                    surveyTotal[0][2] += 1;
                } else {
                    surveyTotal[0][3] += 1;
                }
                int creditEvaluate = survey.getCreditEvaluate();
                if (creditEvaluate == 1) {
                    surveyTotal[1][0] += 1;
                } else if (creditEvaluate == 2) {
                    surveyTotal[1][1] += 1;
                } else if (creditEvaluate == 3) {
                    surveyTotal[1][2] += 1;
                } else {
                    surveyTotal[1][3] += 1;
                }
                int saleEvaluate = survey.getSaleEvaluate();
                if (saleEvaluate == 1) {
                    surveyTotal[2][0] += 1;
                } else if (saleEvaluate == 2) {
                    surveyTotal[2][1] += 1;
                } else if (saleEvaluate == 3) {
                    surveyTotal[2][2] += 1;
                } else {
                    surveyTotal[2][3] += 1;
                }
                int qualityEvaluate = survey.getQualityEvaluate();
                if (qualityEvaluate == 1) {
                    surveyTotal[3][0] += 1;
                } else if (qualityEvaluate == 2) {
                    surveyTotal[3][1] += 1;
                } else if (qualityEvaluate == 3) {
                    surveyTotal[3][2] += 1;
                } else {
                    surveyTotal[3][3] += 1;
                }
            }
            for (int i = 0; i < 4; i++) {
                double sum = 0;
                for (int j = 0; j < 4; j++) {
                    sum += surveyTotal[i][j];
                }
                surveyTotal[i][4] = sum;
            }
            //输出供应商选择人数
            System.out.println("输出供应商选择人数");
            for (int i = 0; i < 4;i++) {
                for (int j = 0;j < 5;j++) {
                    System.out.println(surveyTotal[i][j]+",");
                }
                System.out.println();
            }
            //通过计算构建问卷比例矩阵
            double[][] surveyMatrix = new double[4][4];
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4;j++) {
                    System.out.println("输出surveyTotal  ij"+surveyTotal[i][j]+"   surveyTotal[i][4]"+surveyTotal[i][4]);
                    surveyMatrix[i][j] = surveyTotal[i][j] / surveyTotal[i][4];
                }
            }
            //输出比例矩阵
            System.out.println("输出比例矩阵");
            for (int i = 0; i < 4;i++) {
                for (int j = 0;j < 4;j++) {
                    System.out.println(surveyMatrix[i][j]+",");
                }
                System.out.println();
            }
            //矩阵相乘(将surveyMatrix算出来的比例*Rmatrix各个因素的权重)得出结果
            double[] multiplyResult = new double[4];
            for (int i = 0; i < 4;i++) {
                double sum = 0;
                for (int j = 0; j < 4; j++) {
                    sum += Rmatrix[j][4] * surveyMatrix[j][i];
                }
                multiplyResult[i] = sum;
            }
            //输出评分矩阵相乘结果
            System.out.println("输出评分矩阵相乘结果");
            for (int i = 0;i < 4; i++) {
                System.out.println(multiplyResult[i]+",");
            }
            double evaluateScore = 100 * multiplyResult[0] + 75 * multiplyResult[1] + 50 * multiplyResult[2] + 25 * multiplyResult[3];
            System.out.println("供应商的分数"+evaluateScore);
            if (maxEvaluateScore < evaluateScore) {
                maxEvaluateScore = evaluateScore;
                supplierMaterialResult = supplierMaterial;
            }
            System.out.println("maxEvaluateScore分数"+maxEvaluateScore);
        }
        System.out.println(supplierMaterialResult.getSupplierId());
        Supplier supplier = supplierService.selectById(supplierMaterialResult.getSupplierId());
        return JSONResult.ok(supplier);
    }
}
